﻿using CSRedis;
using System.IO.Compression;
using System.Net;

namespace RedisDemo
{
    internal class Program
    {
        public static void CompressZipFile(string sourceFilePath, string zipFilePath) 
        {
            // 确保指定的路径中的目录存在
            DirectoryInfo directoryInfo = new DirectoryInfo(zipFilePath);
            if (directoryInfo.Parent != null) 
            {
                directoryInfo = directoryInfo.Parent;
            }
            if (!directoryInfo.Exists) 
            {
                directoryInfo.Create();
            }

            using (ZipArchive archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Update))
            {
                archive.CreateEntryFromFile(sourceFilePath, Path.GetFileName(sourceFilePath));
            }
            Console.WriteLine("文件压缩完成");
        }

        static void Main(string[] args)
        {
            var sourceFilePath = @".\MySourceFile.xls";
            var zipSourceFilePath = @".\OutputFolder\ZipSourceFilePath.zip";
            CompressZipFile(sourceFilePath, zipSourceFilePath);
            Console.WriteLine("操作完成");


            //var d = IPAddress.NetworkToHostOrder(BitConverter.ToDouble(new byte[] { 0x40, 0x4C, 0xbb, 0x30, 0xa0, 0x00, 0x00, 0x00 }));


            //return;




            //// 初始化RedisHelper对象
            //var csredis = new CSRedisClient("127.0.0.1:6379,defaultDatabase=0,prefix=_hetai_");
            ////var csredis = new CSRedisClient("127.0.0.1:6379,defaultDatabase=0");
            //RedisHelper.Initialization(csredis);

            //var classrooms = new[]
            //{
            //    new Student{ Id = 1, Name = "张三", Age = 23 },
            //    new Student{ Id = 2, Name = "李四", Age = 26 },
            //    new Student{ Id = 3, Name = "王五", Age = 28 },
            //};
            //var extraStudent = new Student { Id = 4, Name = "刘六", Age = 30 };


            //string key = "Classrooms_Set";
            //csredis.Del(key);
            //csredis.SAdd(key, classrooms[0], classrooms[1], classrooms[2]);

            ////var classroom_set = csredis.SMembers<Student>(key);

            ////classroom_set[1] = extraStudent;

            ////csredis.SRem(key, "{\"Id\":2,\"Name\":\"李四\",\"Age\":26}"); 
            //csredis.SRem(key, "{\"Id\":2,\"Name\":\"李四\",\"Age\":26}");
            ////csredis.SRem(key, new Student { Id = 2, Age = 26, Name = "李四" });

            //csredis.SAdd(key, extraStudent);

            //var query = csredis.SScan<Student>(key, 0, "*王五*", 10000);


            //string key = "Classrooms_List";
            //csredis.Del(key);
            //csredis.RPush(key, classrooms[0], classrooms[1], classrooms[2]);

            //var classroom_list = csredis.LRange<Student>(key, 0, -1);
            ////classroom_list[1] = student_string_1;

            ////csredis.RPush(key, classroom_list);

            //csredis.LRem(key, 0, classrooms[1]);


            //string key = "Classrooms_ZSet";
            //csredis.Del(key);
            //csredis.ZAdd(key, (4681755698536598446, classrooms[0]));
            //csredis.ZAdd(key, (5507322626759218444, classrooms[1]));
            //csredis.ZAdd(key, (5507322626759218333, classrooms[2]));
            //csredis.ZRemRangeByScore(key, 5507322626759218444, 5507322626759218444);

            //;





            //var deviceData = csredis.Scan(0, "_hetai_lastDeviceData:*", null);
            //foreach (var lastDeviceData in deviceData.Items) 
            //{
            //    Console.WriteLine($"{lastDeviceData}");
            //}


            //var alarmData = csredis.Scan(0, "_hetai_lastAlarm:*", null);
            //foreach (var lastAlarmData in alarmData.Items)
            //{
            //    Console.WriteLine($"{lastAlarmData}");
            //}

            //var studentData = csredis.Scan(0, "*Student*", null);
            //foreach (var studentDataItem in studentData.Items)
            //{
            //    Console.WriteLine($"{studentDataItem}");
            //}

            //var allKeys = csredis.Keys("_hetai_lastAlarm:*");
            //var alarmData = csredis.Scan(0, "_hetai_lastAlarm:*", 10000000000);
            //var deviceData = csredis.Scan(0, "_hetai_lastDeviceData:*", 10000000000);
            //var studentData = csredis.Scan(0, "_hetai_Student:*", 10000000000);
            //var allDatas = csredis.Scan(0, count:1000);

            //var setStudDatas = csredis.SScan<Student>("Classrooms_Set", 0, "*张三*", 1000);

            //var hastStudDatas = csredis.HScan<Student>("Student_10001", 0, "*334", 1000);

            // var xx = csredis.MGet<Student>(new [] { "_hetai_Student:1", "_hetai_Student:2", "_hetai_Student:3", "_test_Student:1", "_test_Student:2", "_test_Student:3", "_test_Student:4" });

            //var xx = csredis.Get<Student>("_hetai_Student:*");
            //;
            //var alarmDatas = csredis.Scan(0, "_hetai_lastAlarm:*", 10000);
            //;


            //var classroom_keys = new[]
            //{
            //    "Student:1",
            //    "Student:2",
            //    "Student:3",
            //};
            //var classrooms = new[]
            //{
            //    new Student{ Id = 1, Name = "张三", Age = 23 },
            //    new Student{ Id = 2, Name = "李四", Age = 26 },
            //    new Student{ Id = 3, Name = "王五", Age = 28 },
            //};

            //var extraStudent = new Student { Id = 2, Name = "刘六", Age = 30 };

            //// -------------字符串(string)----------------
            //csredis.Set(classroom_keys[0], classrooms[0]);
            //csredis.Set(classroom_keys[1], classrooms[1]);
            //csredis.Set(classroom_keys[2], classrooms[2]);

            //var student_string_0 = csredis.Get<Student>(classroom_keys[0]);
            //var student_string_1 = csredis.Get<Student>(classroom_keys[1]);
            //var student_string_2 = csredis.Get<Student>(classroom_keys[2]);


            //// 移除元素
            ////csredis.Del(classroom_keys[1]);
            //csredis.Set(classroom_keys[1], extraStudent);
            ///*    数值操作    */
            //csredis.Set("num-key", "24");

            //// value += 5
            //csredis.IncrBy("num-key", 5);
            ////// output -> 29

            ////// value -= 10
            //csredis.IncrBy("num-key", -10);
            ////// output -> 19



            //// -------------哈希(hash)----------------
            //RedisHelper.HSet("Student_10001", "4681755698536598446", classrooms[0]); // 冒号的作用相当于创建一个文件夹
            //RedisHelper.HSet("Student_10001", "5507322626759218237", classrooms[1]);
            //RedisHelper.HSet("Student_10001", "5507322626759218334", classrooms[2]);

            //RedisHelper.HSet("Student_10001", "5507322626759218237", extraStudent);
            //var user10001 = RedisHelper.HGetAll<Student>("User:10001");






            ////-----------------列表(list)----------------
            //csredis.Del("Classrooms_List");
            //csredis.RPush("Classrooms_List", classrooms[0], classrooms[1], classrooms[2]);

            //var classroom_list = csredis.LRange<Student>("Classrooms_List", 0, -1);
            //classroom_list[1] = student_string_1;

            //csredis.Del("Classrooms_List");
            //csredis.RPush("Classrooms_List", classroom_list);





            ////------------------集合(set)----------------
            //// 实际上只插入了两个元素("item1","item2")
            //csredis.Del("Classrooms_Set");
            //csredis.SAdd("Classrooms_Set", classrooms[0], classrooms[1], classrooms[2]);

            //var classroom_set = csredis.SMembers<Student>("Classrooms_Set");

            //classroom_set[1] = extraStudent;

            //csredis.SRem("Classrooms_Set", classrooms[1]);

            //csredis.SAdd("Classrooms_Set", extraStudent);

            //csredis.SScan<Student>("Classrooms_Set*", 0, "");



            ////------------------有序集合(set)----------------

            //RedisHelper.ZAdd("sorted_set", (4681755698536598446, classrooms[0]));
            //RedisHelper.ZAdd("sorted_set", (5507322626759218237, classrooms[1]));
            //RedisHelper.ZAdd("sorted_set", (5507322626759218237, classrooms[2]));

            //// -------------字符串(string)----------------
            //csredis.Set("hello", new Student { Name = "许仲野" });
            //csredis.Set("world", new Student { Name = "张三" });
            //csredis.Set("hello", new Student { Name = "李四" });

            //// 根据键获取对应的值
            //var my = csredis.Get<Student>("hello");

            //// 移除元素
            //csredis.Del("world");

            ///*    数值操作    */
            //csredis.Set("num-key", "24");

            //// value += 5
            //csredis.IncrBy("num-key", 5);
            //// output -> 29

            //// value -= 10
            //csredis.IncrBy("num-key", -10);
            //// output -> 19
            //;
            ///*    字节串操作    */
            //csredis.Set("string-key", "hello ");

            //// 在指定key的value末尾追加字符串
            //csredis.Append("string-key", "world");
            //// output -> "hello world"

            //// 获取从指定范围所有字符构成的子串（start:3,end:7）
            //csredis.GetRange("string-key", 3, 7);
            //// output ->  "lo wo"

            //// 用新字符串从指定位置覆写原value（index:4）
            //csredis.SetRange("string-key", 4, "aa");
            //// output -> "hellaaword"





            ////-----------------列表(list)----------------
            //// 从右端推入元素
            //csredis.RPush("my-list", "item1", "item2", "item3", "item4");
            //// 从右端弹出元素
            //csredis.RPop("my-list");
            //// 从左端推入元素
            //csredis.LPush("my-list", "LeftPushItem");
            //// 从左端弹出元素
            //csredis.LPop("my-list");

            //// 遍历链表元素（start:0,end:-1即可返回所有元素）
            //foreach (var item in csredis.LRange("my-list", 0, -1))
            //{
            //    Console.WriteLine(item);
            //}
            //// 按索引值获取元素（当索引值大于链表长度，返回空值，不会报错）
            //Console.WriteLine($"{csredis.LIndex("my-list", 1)}");

            //// 修剪指定范围内的元素（start:4,end:10）
            //csredis.LTrim("my-list", 4, 10);

            //// 将my-list最后一个元素弹出并压入another-list的头部
            //csredis.RPopLPush("my-list", "another-list");




            ////------------------集合(set)----------------
            //// 实际上只插入了两个元素("item1","item2")
            //csredis.SAdd("my-set", "item1", "item1", "item2");

            //// 集合的遍历
            //foreach (var member2 in csredis.SMembers("my-set"))
            //{
            //    Console.WriteLine($"集合成员：{member2.ToString()}");
            //}

            //// 判断元素是否存在
            //string member = "item1";
            //Console.WriteLine($"{member}是否存在:{csredis.SIsMember("my-set", member)}");
            //// output -> True

            //// 移除元素
            //csredis.SRem("my-set", member);
            //Console.WriteLine($"{member}是否存在:{csredis.SIsMember("my-set", member)}");
            //// output ->  False

            //// 随机移除一个元素
            //csredis.SPop("my-set");

            //csredis.SAdd("set-a", "item1", "item2", "item3", "item4", "item5");
            //csredis.SAdd("set-b", "item2", "item5", "item6", "item7");

            //// 差集
            //csredis.SDiff("set-a", "set-b");
            //// output -> "item1", "item3","item4"

            //// 交集
            //csredis.SInter("set-a", "set-b");
            //// output -> "item2","item5"

            //// 并集
            //csredis.SUnion("set-a", "set-b");
            //// output -> "item1","item2","item3","item4","item5","item6","item7"





            ////------------------散列(hashmap)----------------
            //// 向散列添加元素
            //csredis.HSet("ArticleID:10001", "Title", "了解简单的Redis数据结构");
            //csredis.HSet("ArticleID:10001", "Author", "xscape");
            //csredis.HSet("ArticleID:10001", "PublishTime", "2019-01-01");

            //// 根据Key获取散列中的元素
            //csredis.HGet("ArticleID:10001", "Title");

            //// 获取散列中的所有元素
            //foreach (var item in csredis.HGetAll("ArticleID:10001"))
            //{
            //    Console.WriteLine(item.Value);
            //}

            ////HMGet和HMSet是他们的多参数版本，一次可以处理多个键值对
            //var keys = new string[] { "Title", "Author", "publishTime" };
            //csredis.HMGet("ID:10001", keys);

            ////和处理字符串一样，我们也可以对散列中的值进行自增、自减操作，原理同字符串是一样的
            //csredis.HSet("ArticleID:10001", "votes", "257");
            //csredis.HIncrBy("ID:10001", "votes", 40);
            //// output -> 297



        }
    }
    public class Student : IEquatable<Student>
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }

        public bool Equals(Student? other)
        {
            if (other == null) 
            {
                return false;
            }
            return Id == other.Id;
        }

        public override int GetHashCode()
        {
            return Id;
        }

        public override string ToString()
        {
            return $"Id:{Id},Age:{Age},Name:{Name}";
        }
    }
}
